package com.designoutput.ui.toolGroup
{
	import com.designoutput.enum.Orientation;
	import com.designoutput.enum.ToolType;
	import com.designoutput.event.CardEvent;
	import com.designoutput.event.CardLayerEvent;
	import com.designoutput.event.NumberEvent;
	import com.designoutput.icon.LandscapeIcon;
	import com.designoutput.icon.PortraitIcon;
	import com.designoutput.manager.ResourceManager;
	import com.designoutput.ui.Card;
	import com.designoutput.ui.CardFace;
	import com.designoutput.ui.LeftToolBar;
	import com.designoutput.ui.controls.BoxSplit;
	import com.designoutput.ui.controls.Literal;
	import com.designoutput.ui.controls.NumberInput;
	import com.designoutput.ui.controls.ToolButton;
	import com.designoutput.ui.newControls.ControlBase;
	import com.designoutput.utils.Globals;
	import com.designoutput.vo.CardTypeVo;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.events.MouseEvent;
	
	public class LayerSizeGroup extends ControlBase
	{
		private var _card:Card;
		private var _layer:CardFace;
		
		private var _portraitBtn:ToolButton;
		private var _landscapeBtn:ToolButton;
		private var _widthInput:NumberInput;
		private var _heightInput:NumberInput;
		
		override protected function initialize():void {
			super.initialize();
			
			_card = Globals.designoutputCard.card;
			
			setLayer(_card.currentLayer);
			_card.addEventListener(CardEvent.CURRENT_LAYER_CHANGED, card_currentLayerChanged, false, 0, true);
			
			createChildren();
			readData();
		}
		
		private function card_currentLayerChanged(e:CardEvent):void {
			setLayer(_card.currentLayer);
		}
		
		private function setLayer(layer:CardFace):void {
			if(_layer != null){
				_layer.removeEventListener(CardLayerEvent.UNIT_CHANGED, card_unitChanged, false);
				_layer.removeEventListener(CardLayerEvent.SIZE_CHANGED, card_sizeChanged, false);
			}
			
			_layer = layer;
			
			if(_layer != null){
				_layer.addEventListener(CardLayerEvent.UNIT_CHANGED, card_unitChanged, false, 0, true);
				_layer.addEventListener(CardLayerEvent.SIZE_CHANGED, card_sizeChanged, false, 0, true);
			}
		}
		
		private function createChildren():void{
			var split:BoxSplit = new BoxSplit(BoxSplit.MEDIUMTYPE);
			split.x = 20;
			split.y = 10;
			this.addChild(split);
			
			var li:Literal = new Literal("W");
			li.x = 7;
			li.y = 20;
			this.addChild(li);
			_widthInput = new NumberInput(35, 0, 0, 100000, true, true);
			_widthInput.addEventListener(NumberEvent.VALUE_CHANGE, this.widthInput_changedHandler);
			_widthInput.x = 25;
			_widthInput.y = 20;
			this.addChild(_widthInput);
			
			li = new Literal("H");
			li.x = 70;
			li.y = 20;
			this.addChild(li);
			_heightInput = new NumberInput(35, 0, 0, 100000, true, true);
			_heightInput.addEventListener(NumberEvent.VALUE_CHANGE, this.heightInput_changedHandler);
			_heightInput.x = 90;
			_heightInput.y = 20;
			this.addChild(_heightInput);
			
			_portraitBtn = new ToolButton(ToolType.Portrait, new PortraitIcon(0, 0), 2, 3);
			_portraitBtn.addEventListener(MouseEvent.MOUSE_DOWN, this.orientation_clickHandler);
			_portraitBtn.addEventListener(MouseEvent.MOUSE_OVER, this.toolMouseOver);
			_portraitBtn.addEventListener(MouseEvent.MOUSE_OUT, Globals.designoutputCard.hover.hideEvent);
			_portraitBtn.x = 35;
			_portraitBtn.y = 50;
			this.addChild(_portraitBtn);
			
			_landscapeBtn = new ToolButton(ToolType.Landscape, new LandscapeIcon(0, 0), 1, 3);
			_landscapeBtn.addEventListener(MouseEvent.MOUSE_DOWN, this.orientation_clickHandler);
			_landscapeBtn.addEventListener(MouseEvent.MOUSE_OVER, this.toolMouseOver);
			_landscapeBtn.addEventListener(MouseEvent.MOUSE_OUT, Globals.designoutputCard.hover.hideEvent);
			_landscapeBtn.x = 75;
			_landscapeBtn.y = 50;
			this.addChild(_landscapeBtn);
		}
		
		protected function card_sizeChanged(e:Event):void {
			readData();
		}
		
		protected function card_unitChanged(e:Event):void {
			readData();
		}
		
		/**
		 * Change card size when type in the customer size
		 * @param evt
		 */
		private function widthInput_changedHandler(evt:Event):void
		{
			applyChanges();
		}
		
		/**
		 * Change card size when type in the customer size
		 * @param evt
		 */
		private function heightInput_changedHandler(evt:Event):void
		{
			applyChanges();
		}
				
		private function orientation_clickHandler(evt:MouseEvent):void
		{
			var toolButton:ToolButton = evt.currentTarget as ToolButton;
			if(toolButton.type == ToolType.Portrait){
				_card.changeLayerOrientation(_card.currentLayer, Orientation.PORTRAIT);
			} else {
				_card.changeLayerOrientation(_card.currentLayer, Orientation.LANDSCAPE);
			}
		}
		
		public function readData():void
		{
			if(!_layer){
				visible = false;
				return;
			}
			
			visible = _layer.allowCustomSize;
			
			_widthInput.value = _layer.widthInUnit;
			_heightInput.value = _layer.heightInUnit;
			
			if(_layer.orientation == Orientation.PORTRAIT){
				_portraitBtn.Mark(true);
				_landscapeBtn.Mark(false);
			} else {
				_portraitBtn.Mark(false);
				_landscapeBtn.Mark(true);
			}
		}
		
		public function applyChanges():void{
			var width:Number = Number(_widthInput.value);
			var height:Number = Number(_heightInput.value);
			
			if(!isNaN(width) && !isNaN(height)){
				_card.changeLayerSize(_layer, width, height);
			}
		}
		
		private function toolMouseOver(evt:MouseEvent):void
		{
			var type:String = (evt.currentTarget as ToolButton).type;
			switch (type)
			{
				case ToolType.Portrait:
					Globals.designoutputCard.hover.setText(ResourceManager.getValue("rm_portrait"));
					break;
				case ToolType.Landscape:
					Globals.designoutputCard.hover.setText(ResourceManager.getValue("rm_landscape"));
					break;
				default:
					break;
			}
		}
	}
}